/*
 * NetworkDialogue - pretty bubble that tells you the connection failed
 *
 * Copyright 2007 James W. Hofmann
 *
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.Lib;

class NetworkDialogue
{

	static var img : Sprite;
	static var connectext : Bitmap;

	static var finishdelay : Int = -1;
	static var msglength : Int = -1;
	static public var busy : Bool = false;
	
	static public function start()
	{
		img = new Sprite();
		redrawrect();
	
		connectext = new Bitmap();
		write(["Network transaction..."]);
		
		img.addChild(connectext);
		
		Lib.current.addChild(img);
		
		busy = true;
	}

	static public function redrawrect()
	{
		var width = 400;
		var height = 60;

		if (msglength>0)
		{
			width = connectext.bitmapData.width + 16;
		}
		
		img.graphics.clear();
		img.graphics.lineStyle(2,0xFFFFFF,0.75);
		img.graphics.beginFill(0x888888,0.75);

		img.graphics.drawRoundRect((680/2)-(width/2),(425/2)-(height/2),width,height,32,32);
	}
	
	static public function bringup()
	{
		try
		{
			Lib.current.setChildIndex(img, Lib.current.numChildren-1);
		}
		catch (foo : Dynamic) {}
	}
	
	static public function write(str : Array<Dynamic>)
	{
		connectext.bitmapData = Fonts.verdanaitalic.gen(str);
		connectext.x = (680/2)-(connectext.bitmapData.width/2) + 4;
		connectext.y = (425/2)-connectext.bitmapData.height/2;
		msglength = 0;
		for (n in str)
		{
			msglength+=Std.string(n).length;
		}
		redrawrect();
	}

	static public function finish()
	{
		Main.timer.addEventListener(flash.events.TimerEvent.TIMER, exit);
		finishdelay = 25;
	}

	static public function exit(evt : flash.events.Event)
	{
		finishdelay--;
		if (finishdelay<0)
		{
			Main.timer.removeEventListener(flash.events.TimerEvent.TIMER, exit);
			try
			{
				Lib.current.removeChild(img);
			}
			catch(foo: Dynamic) {}
			finishdelay = -1;
			busy = false;
		}
	}

}
